$(document).ready(function () {
    var selfUserId = ""
    function searchFun() {
        if ($("#searchValue").val()) {
            $.ajax({
                url: 'http://localhost:5000/friendsinfo/' + $("#searchValue").val(),
                type: 'get',
                success: function (data, status) {
                    var sex = data.sex === 'boy' ? '男♂' : '女♀'
                    var status = data.status === 'down' ? '离线' : '在线'
                    var searchHtml = '<ul><li>有聊号：' + data.account
                        + '</li><li>用户名：' + data.name
                        + '</li><li>性别:' + sex
                        + '</li><li>' + status
                        + '</li><li>签名：'
                        + data.signature + '</li></ul>'
                        + '<button id="addFclick">添加</button>'
                    $("#seachNameId").val(data._id)
                    $("#searchInforBox").html(searchHtml)
                },
                error: function (data, status) {
                    if (status == 'error') {
                        console.log('33333333333333')
                    }
                }
            });
        } else {
            console.log('err')
        }
    }
    $("#searchBtn").click(searchFun)
    $("#searchInforBox").on('click', '#addFclick', function () {
        console.log($("#seachNameId").val())
        $.ajax({
                url: 'http://localhost:5000/reqfriends',
                type: 'post',
                data:{addUser:$("#seachNameId").val(),userId: selfUserId},
                success: function (data, status) {
                    console.log('88888888888888888888888')
                },
                error: function (data, status) {
                    if (status == 'error') {
                        console.log('33333333333333')
                    }
                }
            })
    })

    function friendMhtml(arg) {
        var divhtml = ''
        for (var k = 0; k < arg.group.length; k++) {
            var ulhtml = ''
            for (var j = 0; j < arg.friends.length; j++) {
                if (arg.friends[j].groupId === arg.group[k]._id) {
                    ulhtml = ulhtml + '<li>' +
                        '<img src="./content/img/1.jpg"><p>' +
                        arg.friends[j].user.name
                        + '</p><i class="fa fa-times" aria-hidden="true"></i>'
                        + '</li>'
                }
            }
            divhtml = divhtml + '<span><i class="fa fa-angle-down" aria-hidden="true"></i><p>' + arg.group[k].gname + '</p></span><ul>' + ulhtml + '</ul>'
        }
        return divhtml
    }
    function inforHtmlDiv(data){
        var inforHtml = ''
        for (let z = 0;z < data.length; z++){
            inforHtml = inforHtml +
            '<ul><li><span class="glyphicon glyphicon-bell" aria-hidden="true"></span> ' + 
            '<p>' + data[z].name + '请求成为好友！</p><br/>' +
            '<div><button infor=' + data[z]._id +'>同意</button>' +
            '<button infor=' + data[z]._id +'>拒绝</button>' +
            '</div></li></ul>'            
        }
        return inforHtml
    }
    function friendMfun() {
        $.ajax({
            url: 'http://localhost:5000/friends/q',
            type: 'get',
            success: function (data, status) {
                selfUserId = data._id
                $("#manageFriends").html(friendMhtml(data))
                $("#getInforDiv").html(inforHtmlDiv(data.framef))
            },
            error: function (data, status) {
                if (status == 'error') {
                    console.log('33333333333333')
                }
            }
        })
    }
    $("#managemenTabs").click(friendMfun)
    friendMfun()
})